package com.cssw.fyzb.tps.api.sts;

import com.cssw.fyzb.tps.client.StsClient;
import com.cssw.fyzb.tps.model.vo.CredentialsVo;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 退出登录
 *
 * @author Feng Chen
 */
@Function(description = "获取联合token", parameters = {
        @Parameter(name = "policy", type = Type.String, description = "策略", required = true),
}, result = @Result(properties = {
        @Property(name = "sessionToken", type = Type.String, description = "会话token"),
        @Property(name = "tmpSecretId", type = Type.String, description = "临时secretId"),
        @Property(name = "tmpSecretKey", type = Type.String, description = "临时secretKey"),
        @Property(name = "requestId", type = Type.String, description = "请求编号"),
        @Property(name = "expireTime", type = Type.String, description = "失效时间"),
}))
@Component
public class GetFederationToken implements Action {

    @Autowired
    private StsClient stsClient;

    private static final Logger logger = LoggerFactory.getLogger(GetFederationToken.class);

    @Override
    public Response execute(Request request) throws Exception {
        String policy = request.getString("policy");
        try {
            CredentialsVo vo = stsClient.getFederationToken(policy);
            return Response.success().put("data", vo);
        } catch (Exception e) {
            logger.error("", e);
            return Response.fail("操作异常");
        }
    }

}
